/**
 * @(#)PipeDreamAgent.java
 *
 *
 * @author: Xu Weijie,Choon Meng, Chris 
 * @version 1.00 2009/2/12
 */

import sg.edu.nus.comp.cs3243.pipedream.*;

import java.io.*;
import java.util.*;

public class MyPlayer extends Player{

    // constants that are used for the public accessor functions required by the Player abstract class
	static final String MATRIC_NUMBER = "U057020R";
    static final String NAME = "Xu Weijie, Chris, Choon Meng";
    static final String EMAIL = "u0507020@nus.edu.sg";
    static final String TAGLINE = "Our agent is super intelligent";
    
	byte[] prevPosition = {-1, -1};
	boolean waitMode = false;
    
	public static int FinalScore;

    // empty constructor, no need for this sample class to have anything initialized
    MyPlayer () { ; }

    // publically-available accessor functions
    public String getMatric () { return (MATRIC_NUMBER); }
    public String getName () { return (NAME); }
    public String getEmail () { return (EMAIL); }
    public String getTagLine () { return (TAGLINE); }

    // the play method, this version passes the work to the input operator
    public Action play (Driver d) {
		Action a = new Action();			  // create a new Action object
		char[][] map = new char [7][7];		  // pointer for storing the board
		char[] queue = new char [5];		  // pointer for storing the queue 				  

		map = d.getBoard();					  // read the contents of the board
		queue = d.getQueue();				  // read the contents of the queue

		d.printQueue();						  // print the queue so we can see what's going on
		d.printBoard();						  // same with the board

		if (waitMode)
			a.setAction('w');
		else {

			byte[] position= new byte[2];

			if (prevPosition[0] == -1) {
				HMachine hm = new HMachine ( d );
				position = hm.calculateHeuristics();
			} else {
				position[0] = prevPosition[0];
				position[1] = prevPosition[1];
				prevPosition[0] = -1;
				prevPosition[1] = -1;
			}

			if(position[0] == -1) {
				a.setAction('w');
				waitMode = true;
				// System.out.println("WAITING");
			} else if(map[position[0]][position[1]]==' ')
				a.setAction('p');
			else
			{
				// System.out.println("DESTROYING");
				a.setAction('d');

				// make sure we destroy and make use of this grid straight-away
				prevPosition[0] = position[0];
				prevPosition[1] = position[1];
			}

			a.setX(position[0]);
			a.setY(position[1]);
		}

		return (a);						      // got the action, return it
	}

    // Here's the driver for the entire game.  You can use the same in your program
    // When testing and grading your program, we'll use a variant of this main() method.
    public static void main (String [] args) {
    
	Driver d = new Driver();				      // create a game driver
	MyPlayer p = new MyPlayer();				  // create my game-playing agent
	boolean status = true;					      // initially, the game hasn't ended

	System.out.print ("This is Strategic Pipe Dreams, game playing agent by " + p.getName() + "(" + 
			  p.getMatric() + ")\n<" + p.getEmail() + "> who says: \"" + p.getTagLine() + "\"\n");
	for (; status == true;) {				      // while the game isn't over...
	    Action a = p.play(d);				      // ...get an action from the agent
	    status = d.processTurn(a);				      // ...and process it
	}

	// Game's over, what's my score?
	System.out.print ("My final score for this game was: " + d.getScore() + "\n");
	FinalScore = d.getScore();
    } 
}
